package com.example.sls.service.impl.teacher.pro;

import com.example.sls.mapper.executeSQL.ExecuteSQLMapper;

import com.example.sls.mapper.teacher.pro.ProMapper;
import com.example.sls.pojo.teacher.pro.Pro;
import com.example.sls.service.teacher.pro.ProService;
import com.example.sls.utils.ConstUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProServiceImpl implements ProService {

    @Autowired
    private ProMapper proMapper;  //用于对习题进行操作

    @Autowired
    private ExecuteSQLMapper executeSQLMapper;  //用于获取答案

    @Override
    public boolean judgeAnswer(Integer id, String answer) {

        boolean flag = false;
        Pro pro = proMapper.getProById(id);
        Integer proType = pro.getProType();

//        System.out.println("========");
//        System.out.println("id: " + id);
//        System.out.println("pro: " + pro);
//        System.out.println("proType: " + proType);
//        System.out.println("========");

        String ans = null;

        if(proType == 1){
            try{
                ans = executeQuerySQL(answer);
            }catch(Exception exception){
                return false;
            }
            if(ans == null){
                return false;
            }
            else if(ans.equals(pro.getResult())){
                flag = true;
            }

        }

        return flag;
    }


    @Override
    public List<Pro> getProsBySetId(Integer setId) {
        return proMapper.getProsBySetId(setId);
    }

    //分页获取题目信息
    @Override
    public PageInfo<Pro> getPageInfoBySetId(Integer setId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Pro> pros = proMapper.getProsBySetId(setId);
        return new PageInfo<Pro>(pros);
    }

    @Override
    public Pro getProById(Integer id) {
        return proMapper.getProById(id);
    }

    @Override
    public Pro getProInfoById(Integer id) {
        return proMapper.getProInfoById(id);
    }

    @Override
    public Boolean addPro(Integer setId,Integer proType, String content, String correctSQL) {
        Integer id;
        if(proMapper.getMaxId() == null){
            id = 1;
        }
        else{
            id = proMapper.getMaxId() + 1;
        }

        String result = executeQuerySQL(correctSQL);
        Pro pro =
                new Pro(id,setId,proType,content,correctSQL,result);

        boolean flag = false;
        if(proMapper.addPro(pro) > 0){
            flag = true;
        }
        return flag;
    }

    @Override
    public List<Pro> getProsInfo() {
        return proMapper.getProsInfo();
    }

    @Override
    public Integer getCount() {
        return proMapper.getCount();
    }

    @Override
    public Integer getMaxId() {
        return proMapper.getMaxId();
    }






    public String executeQuerySQL(String correctSQL){

        String result = null ;

        if(correctSQL.startsWith("select") || correctSQL.startsWith("SELECT")){
            String condition = correctSQL.split("\\s+")[1]; //剔除用户输入的多个空格
            System.out.println("condition: " + condition);
            System.out.println("========");
            if(condition.equals("*")){  //查询所有
                String temp = String.valueOf(executeSQLMapper.queryExecuteAll(correctSQL));
                result = executeJudge(temp);
            }
            else if(condition.startsWith("sum") || condition.startsWith("SUM")){  //求和
                String temp = String.valueOf(executeSQLMapper.queryExecuteSum(correctSQL));
                result = executeJudge(temp);
            }
            else if(condition.startsWith("count") || condition.startsWith("COUNT")){  //求数量
                String temp = String.valueOf(executeSQLMapper.queryExecuteCount(correctSQL));
                result = executeJudge(temp);
            }
            else if(condition.startsWith("name") || condition.startsWith("NAME")
                    || condition.startsWith("idcard") || condition.startsWith("IDCARD")
                    || condition.startsWith("address") || condition.startsWith("ADDRESS")){
                String temp = String.valueOf(executeSQLMapper.queryExecuteStr(correctSQL));
                result = executeJudge(temp);
            }
            else if(condition.startsWith("age") || condition.startsWith("AGE")
                    || condition.startsWith("gender")||condition.startsWith("GENDER")){
                String temp = String.valueOf(executeSQLMapper.queryExecuteInt(correctSQL));
                result = executeJudge(temp);
            }
            else if(condition.startsWith("entrydate") || condition.startsWith("ENTRYDATE")){
                String temp = String.valueOf(executeSQLMapper.queryExecuteDate(correctSQL));
                result = executeJudge(temp);
            }
        }
        else{
            result = null;
        }

        return result;
    }


    public String executeJudge(String temp){
        String result;
        if(temp == null){
            result = ConstUtil.MISS;
        }
        else{
            result = temp;
        }
        return result;
    }
}
